package com.java46.admin.user.dao;

import com.java46.admin.base.exception.BusinessException;
import com.java46.admin.base.util.HibernateUtils;
import com.java46.admin.user.entity.UsersEntity;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import java.util.List;

public class UserDaoT {
    /**
     * 查询所有用户
     * @param offset 当前页
     * @param length 每页显示条数
     * @param openId 模糊查询条件
     * @return
     */
    public List<UsersEntity> queryForPage(int offset, int length,String openId) {
        if(openId==null){
            openId="";
        }
        // TODO Auto-generated method stub
        List<UsersEntity> entitylist=null;
        Session session = HibernateUtils.getSession();
        try{
            Query query = session.createQuery("from com.java46.admin.user.entity.UsersEntity as a where a.userState=0 and a.openId like" + " '%"+openId+"%'");
            query.setFirstResult(offset);
            query.setMaxResults(length);
            entitylist = query.list();

        }catch(RuntimeException re){
            throw re;
        }finally {
            session.close();
        }

        return entitylist;
    }

    /**
     * 查询用户总人数
     * @param openId 模糊查询条件
     * @return
     */
    public int getAllRowCount(String openId){
        if(openId==null){
            openId="";
        }
        int count=0;
        Session session = HibernateUtils.getSession();
        try{
            String hql = "select count(*)  from com.java46.admin.user.entity.UsersEntity  as a where a.userState=0 and a.openId like" + " '%"+openId+"%'";
            Query query = session.createQuery(hql);
            count = ((Long)(query.iterate().next())).intValue();
        }catch(RuntimeException re){
            throw re;
        }finally {
            session.close();
        }
        return count;
    }

    /**
     * 删除用户
     * @param openId 用户Id
     * @return
     */
    public int removeUser(String openId){
        int row = 0;
        Session session = HibernateUtils.getSession();
        try{
            //开启事务
            Transaction transaction = session.beginTransaction();
            String hql ="update UsersEntity user set user.userState=1 where user.openId =?1";
            Query query = session.createQuery(hql);
            query.setParameter(1,openId);
            row = query.executeUpdate();
            //提交事务
            transaction.commit();
        }catch(RuntimeException re){
            throw re;
        }finally {
            session.close();
        }
        return row;
    }
    //根据订单上的用户ID查询用户
    public UsersEntity getUserByUserId(int userId){
        Session session = HibernateUtils.getSession();
        try{
            Query query = session.createQuery("from com.java46.admin.user.entity.UsersEntity  as a where a.userId=?1");
            query.setParameter(1,userId);
            UsersEntity usersEntity =(UsersEntity) query.uniqueResult();
            return usersEntity;
        }catch(RuntimeException re){
            throw re;
        }finally {
            session.close();
        }
    }
}
